/*
 * @version: 
 * @Author: yu
 * @Date: 2023-07-10 15:51:00
 * @LastEditors: Andy
 * @LastEditTime: 2023-07-13 15:27:11
 */
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'


const routes: Array<RouteRecordRaw> = [
  {
    path: '/index',
    component: () => import('../pages/Index/index.vue'),
    children: [
      {
        path: 'home',
        component: () => import('../views/home/home.vue'),
        name: '首页',
        meta: { title: '首页' }
      },
      {
        path: 'menu',
        component: () => import('../views/menu/menu.vue'),
        name: '菜单管理',
        meta: { title: '菜单管理' }
      },
      {
        path: 'role',
        component: () => import('../views/role/role.vue'),
        name: '角色管理',
        meta: { title: '角色管理' }
      },
      {
        path: 'member',
        component: () => import('../views/member/member.vue'),
        name: '管理员管理',
        meta: { title: '管理员管理' }
      },
      {
        path: 'cate',
        component: () => import('../views/cate/cate.vue'),
        name: '商品分类',
        meta: { title: '商品分类' }
      },
      {
        path: 'specs',
        component: () => import('../views/specs/specs.vue'),
        name: '商品规格',
        meta: { title: '商品规格' }
      },
      {
        path: 'goods',
        component: () => import('../views/goods/goods.vue'),
        name: '商品管理',
        meta: { title: '商品管理' }
      },
      {
        path: '/index',
        redirect: '/index/home'
      }
    ]
  },
  {
    path: '/login',
    component: () => import('../pages/Login/login.vue'),
    name: '登录页',
    meta: { title: '登录页' }

  },
  {
    path: '/:notfound(.*)*',
    redirect: '/login',
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})


//全局守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    let user = JSON.parse(sessionStorage.getItem('user') as any) || null;
    if (user) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
